<?php
/**
 * Author: DRERY <rui1642@foxmail.com>
 */
namespace backend\models;

use yii\base\Model;

class AdminPasswordForm extends Model
{
    public $password;
    public $repeat;

    public function attributeLabels()
    {
        return [
            'password' => '密码',
            'repeat' => '确认密码'
        ];
    }

    public function rules()
    {
        return [
            ['password', 'string', 'length' => [6, 16]],
            ['password', 'match', 'pattern' => '/[0-9]{1}/', 'message' => '密码必须是数字，小写和大写字母组成的6到16位字符'],
            ['password', 'match', 'pattern' => '/[a-z]{1}/', 'message' => '密码必须是数字，小写和大写字母组成的6到16位字符'],
            ['password', 'match', 'pattern' => '/[A-Z]{1}/', 'message' => '密码必须是数字，小写和大写字母组成的6到16位字符'],
            ['repeat', 'compare', 'compareAttribute' => 'password']
        ];
    }

    public function execute(Admin $adminModel)
    {
        if (!$this->validate())
            return false;

        $adminModel->password = $this->password;
        $adminModel->generatePassword();
        return $adminModel->save(false);
    }
}